package com.meiyu.live.tv.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.meiyu.live.R;
import com.meiyu.live.popj.RoomMessage;

import java.util.List;

/**
 * Created by zbl on 2017/1/6.
 */
public class RoomMessageAdapter extends BaseAdapter {

    private static final int TEXT_COLOR_NOTIFY = 0xff8fddfe;
    private static final int TEXT_COLOR_NOMAL = 0xffffffff;

    private LayoutInflater inflater;
    private List<RoomMessage> roomMessageList;

    private View.OnClickListener onNameClickListener;

    class MessageViewHolder {
        View layout_level;
        TextView tv_name, tv_content, tv_level;
    }

    public RoomMessageAdapter(Context context, List<RoomMessage> roomMessageList) {
        this.roomMessageList = roomMessageList;
        inflater = LayoutInflater.from(context);
    }

    public void setOnNameClickListener(View.OnClickListener onNameClickListener) {
        this.onNameClickListener = onNameClickListener;
    }

    @Override
    public int getCount() {
        return roomMessageList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        MessageViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_live_message, null);
            holder = new MessageViewHolder();
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_name.setOnClickListener(onNameClickListener);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.layout_level = convertView.findViewById(R.id.layout_level);
            holder.tv_level = (TextView) convertView.findViewById(R.id.tv_level);
            convertView.setTag(holder);
        } else {
            holder = (MessageViewHolder) convertView.getTag();
        }
        holder.tv_name.setTag(position);
        RoomMessage message = roomMessageList.get(position);
        if (message.getType() == 0) {
            holder.tv_name.setVisibility(View.VISIBLE);
            holder.layout_level.setVisibility(View.VISIBLE);
            holder.tv_name.setText(message.getName());
            holder.tv_content.setTextColor(TEXT_COLOR_NOMAL);
            holder.tv_content.setText(message.getContent());
            holder.tv_level.setText(message.getLevel() + "");
        } else if (message.getType() == 1) {
            holder.tv_name.setVisibility(View.VISIBLE);
            holder.layout_level.setVisibility(View.VISIBLE);
            holder.tv_name.setText(message.getName());
            holder.tv_content.setTextColor(TEXT_COLOR_NOTIFY);
            holder.tv_content.setText(message.getContent());
            holder.tv_level.setText(message.getLevel() + "");
        } else if (message.getType() == 2) {
            holder.tv_name.setVisibility(View.GONE);
            holder.layout_level.setVisibility(View.GONE);
            holder.tv_content.setTextColor(TEXT_COLOR_NOTIFY);
            holder.tv_content.setText(message.getContent());
        }
        return convertView;
    }
}

